﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.SubPage
{
    public partial class uc_trong_tai : System.Web.UI.UserControl
    {
        #region[Define]
        private DataTable dtBockVehicle;
        protected string SuggestionList;
        protected string StrAlert = "";
        private clsTMBock_VehicleBusiness clsBockVehicle = new clsTMBock_VehicleBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtMaTrongTai.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã trọng tải !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (txtChieuDai.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào chiều dài trọng tải !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (txtChieuRong.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào chiều rộng !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (txtChieuCao.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào chiều cao trọng tải !";
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
            if (txtMaTrongTai.Text != "" && txtChieuDai.Text != "" && txtChieuRong.Text != "" && txtChieuCao.Text != "")
            {
                InsertBlockVehicle();
            }
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbao");
        }
        protected void ImgRefeshDuyet_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImgRefeshExport_Click(object sender, ImageClickEventArgs e)
        {
            //txtTuNgayNhapKhoExport.Text = "";
            //txtDenNgayNhapKhoExport.Text = "";
            //BindGridLenhDieuDongExportExcel();
        }
        protected void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {

        }
        //---------Dropdownlist-----------
        protected void drTrongTai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        #endregion

        #region[Function]
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtBockVehicle = clsBockVehicle.BindGirdTMBock_Vehicle();
            if (dtBockVehicle != null && dtBockVehicle.Rows.Count > 0)
            {
                foreach (DataRow dr in dtBockVehicle.Rows)
                {
                    if (txtMaTrongTai.Text == dr["Block_Vehicel_Alias"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertBlockVehicle()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã trọng tải này đã tồn tại !";
                    OpenDialog("#ucthongbao");
                    updatePanelThongBao.Update();
                }
                else
                {
                    VanTaiProperty.clsTMBock_VehicleProperty obj = new clsTMBock_VehicleProperty();
                    obj.Block_Vehicel_Alias = txtMaTrongTai.Text;
                    obj.Blick_Vehicle_Length = float.Parse(txtChieuDai.Text.Replace(",", "."));
                    obj.Block_Vehicel_Width = float.Parse(txtChieuRong.Text.Replace(",", "."));
                    obj.Block_Vehicel_Hight = float.Parse(txtChieuCao.Text.Replace(",", "."));
                    VanTaiBLL.clsTMBock_VehicleBusiness.InsertTMBock_Vehicle(obj);
                    lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                    OpenDialog("#ucthongbao");
                    updatePanelThongBao.Update();
                    ClearTextBox();
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#ucthongbao");
                updatePanelThongBao.Update();
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtMaTrongTai.Text = "";
            txtChieuDai.Text = "0";
            txtChieuRong.Text = "0";
            txtChieuCao.Text = "0";
        }
        #endregion
    }
}